package com.sailing.xjpb.web.base.advice;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * 由于前端默认分页是从1开始，这里需要特别处理
 *
 * @author YaoWei
 * createDate: 2022-12-29
 */
public class SailingPageImpl<T> extends PageImpl<T> {

    public SailingPageImpl(List<T> content, Pageable pageable, long total) {
        super(content, pageable, total);
    }

    @Override
    public boolean hasPrevious() {
        Pageable pageable = getPageable();
        if (pageable instanceof SailingPageRequest) {
            return pageable.getPageNumber() - 1 > 0;
        } else {
            return super.hasPrevious();
        }
    }


    /**
     * 用于解决返回last不正确 getNumber 返回实际是 pageable.getPageNumber() 此方法经过重新会+1，
     * 原hasNext方法会+1 这里导致+2了 故修改此方法
     *
     * @return boolean
     * @author 罗志银
     * createDate 2023/9/20 11:41
     * <p>
     * editDescription
     * editUser
     * editDate 2023/9/20 11:41
     */
    @Override
    public boolean hasNext() {
        Pageable pageable = getPageable();
        if (pageable instanceof SailingPageRequest) {
            return super.getNumber() < getTotalPages();
        } else {
            return super.hasNext();
        }
    }

}
